package com.xia.netty.bio;

import java.io.*;
import java.net.Socket;
import java.util.Objects;

/**
 * 基于客户端的程序
 */
public class BioClient {

    public static void main(String[] args) {
        Socket socket = null;
        BufferedReader in = null;
        BufferedWriter out = null;
        try {
            //创建客户端的socket对象
            socket = new Socket("127.0.0.1", 8888);
            //读数据
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

            System.out.println("准备向服务端写数据 ");
            out.write("你好啊,客户端 \n");
            out.flush();

            //接收了 来自客户端的数据
            String s = in.readLine();
            System.out.println("成功接收数据:" + s);


        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (Objects.nonNull(in) ){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
